﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace GameStateManagement
{
    class Animation
    {
        // the image representing a collection of images
        Texture2D spriteStrip;

        // the scale used to display
        float scale;

        // the time since last update
        int elapsedtime;

        // time between frames
        int frameTime;

        // number of frames
        int frameCount;

        // index of current frame
        int currentFrame;

        // frame color
        Color color;

        // area to display animation
        Rectangle sourceRect = new Rectangle();

        // area on screen to display animation
        Rectangle destinationRect = new Rectangle();

        // width of frame
        public int FrameWidth;

        // height of frame
        public int FrameHeight;

        // anmiation state
        public bool Active;

        // Determine whether to loop or not
        public bool Looping;

        // width of frame
        public Vector2 Position;

        public void Initialize(Texture2D texture, Vector2 position, int frameWidth, int frameHeight,
            int frameCount, int frameTime, Color color, float scale, bool looping)
        {
            this.color = color;
            this.FrameWidth = frameWidth;
            this.FrameHeight = frameHeight;
            this.frameCount = frameCount;
            this.frameTime = frameTime;
            this.scale = scale;

            Looping = looping;
            Position = position;
            spriteStrip = texture;

            // set time to zero
            elapsedtime = 0;
            currentFrame = 0;

            // set animation to active
            Active = true;
        }

        public void Update(GameTime gameTime)
        {
            if (Active == false)
                return;

            // update time
            elapsedtime += (int)gameTime.ElapsedGameTime.TotalMilliseconds;

            // if elapsed time greater than frame time advance frame
            if (elapsedtime > frameTime)
            {
                // move to next frame
                currentFrame++;

                // if last frame move to first frame
                if (currentFrame == frameCount)
                {
                    currentFrame = 0;
                    // disable animation if not looping
                    if (Looping == false)
                        Active = false;
                }

                // reset elapsed time
                elapsedtime = 0;
            }

            sourceRect = new Rectangle(currentFrame * FrameWidth, 0, FrameWidth, FrameHeight);

            // find correct frame by multiplying currentFrame by frameWidth
            destinationRect = new Rectangle((int)Position.X - (int)(FrameWidth * scale) / 2,
                (int)Position.Y - (int)(FrameHeight * scale) / 2,
                (int)(FrameWidth * scale),
                (int)(FrameHeight * scale));
        }

        public void Draw(SpriteBatch spriteBatch, float rotation, Vector2 position)
        {
            // draw animation when active
            if (Active)
            {
                spriteBatch.Draw(spriteStrip, destinationRect, sourceRect, color, rotation, 
                    new Vector2(FrameWidth / 2, FrameHeight / 2), SpriteEffects.None, 0);
            }
        }


    }
}
